package com.ymatou.authapp.testcase;

import java.io.IOException;
import java.util.Map;

import org.apache.http.client.ClientProtocolException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.authapp.parameter.LoginAuthBean;
import com.ymatou.authapp.parameter.LoginLimitRequestDtoBean;
import com.ymatou.authapp.service.LoginAuthCall;
import com.ymatou.authapp.service.LoginLimitRequestDtoCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P3;
import com.ymttest.business.service.UserCallService;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.constant.ECode;
/**
*用户在指定时间内登陆失败的次数接口
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_LoginLimitRequestDto{
	private static LoginLimitRequestDtoBean loginlimitrequestdtoBean;
	private static LoginLimitRequestDtoCall loginlimitrequestdtoCall;
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("用户在指定时间内登陆失败的次数接口");
	}

	@Before
	public void caseUp() {
		loginlimitrequestdtoBean=new LoginLimitRequestDtoBean();
		loginlimitrequestdtoCall=new LoginLimitRequestDtoCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(loginlimitrequestdtoCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_LoginLimitRequestDto_001() {
		Logger.start(true,"返回3分钟内用户登录失败的次数");
		try {
			Map<String, Comparable> user = UserCallService.registerByMobile();
			String loginId = (String)user.get("username");
			loginFailed(loginId, 1);
			loginlimitrequestdtoBean.setLoginId(loginId);
			loginlimitrequestdtoBean.setTimeSpan(-3);
			loginlimitrequestdtoCall.setData(loginlimitrequestdtoBean);
			loginlimitrequestdtoCall.callService();	
			String result = loginlimitrequestdtoCall.getReturnData();
			Logger.verifyEquals("1", result, "验证用户（"+loginId+"）3分钟内登陆失败的次数");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_LoginLimitRequestDto_002() {
		Logger.start(false,"不输入用户ID， 返回0");
		try {
			loginlimitrequestdtoBean.setTimeSpan(-1);
			loginlimitrequestdtoCall.setData(loginlimitrequestdtoBean);
			loginlimitrequestdtoCall.callService();	
			Logger.verifyEquals(ECode.ERROR500, loginlimitrequestdtoCall.getCode(), "验证Code");
			Logger.verifyEquals(true, loginlimitrequestdtoCall.getMsg().contains(ECode.AUTH_INVALID_LOGINID), "验证Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_LoginLimitRequestDto_003() {
		Logger.start(false,"输入错误的ID， 返回0");
		try {
			loginlimitrequestdtoBean.setLoginId("aaaa");
			loginlimitrequestdtoBean.setTimeSpan(-1);
			loginlimitrequestdtoCall.setData(loginlimitrequestdtoBean);
			loginlimitrequestdtoCall.callService();	
			String result = loginlimitrequestdtoCall.getReturnData();
			Logger.verifyEquals("0", result, "验证1分钟内登陆失败的次数");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	@Ignore//现在返回的是过去一分钟内错误的次数
	public void Tc_LoginLimitRequestDto_004() {
		Logger.start(false,"用户未来一分钟内登陆失败的次数， 返回0");
		try {
			Map<String, Comparable> user = UserCallService.registerByMobile();
			String loginId = (String)user.get("username");
			loginFailed(loginId, 3);
			loginlimitrequestdtoBean.setLoginId(loginId);
			loginlimitrequestdtoBean.setTimeSpan(3);
			loginlimitrequestdtoCall.setData(loginlimitrequestdtoBean);
			loginlimitrequestdtoCall.callService();	
			String result = loginlimitrequestdtoCall.getReturnData();
			Logger.verifyEquals("0", result, "验证用户test007一分钟内登陆失败的次数");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	/**
	 * 尝试错误登陆系统
	 * @param loginId 用户ID
	 * @param failedTimes 尝试失败的次数
	 * @throws IOException 
	 * @throws ClientProtocolException 
	 */
	private void loginFailed(String loginId, int failedTimes) throws ClientProtocolException, IOException{
		LoginAuthBean lab;
		LoginAuthCall lac;
		Logger.comment("用户"+loginId+"用错误的密码尝试登陆"+failedTimes+"次");
		for (int i = 0; i < failedTimes; i++) {
			lab = new LoginAuthBean();
			lac = new LoginAuthCall();
			lab.setLoginId(loginId);
			lab.setPas("1234561");
			lab.setClientIp(TestDataManager.getRandomIp());
			lab.setSourceType("MainSite");
			lac.setData(false, lab);
			lac.callService();
		}
	}
	
}